package com.app.utils.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

/*
 * Android调节屏幕亮度工具类
 * by itas109
 * http://blog.csdn.net/itas109
 *
 * 注意：需要添加setting权限
 * <uses-permission android:name="android.permission.WRITE_SETTINGS">
 */
public class BrightnessUtils {

    // 判断是否开启了自动亮度调节

    public static boolean IsAutoBrightness(Context context) {

        boolean IsAutoBrightness = false;

        try {

            IsAutoBrightness = Settings.System.getInt(
                    context.getContentResolver(),

                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;

        } catch (Settings.SettingNotFoundException e)

        {

            e.printStackTrace();

        }

        return IsAutoBrightness;
    }

    /**
     * 设置当前屏幕亮度的模式
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
     */
    public static void setBrightnessMode(Activity activity, int brightMode) {
        Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightMode);
    }

    // 获取当前屏幕的亮度

    public static int getScreenBrightness(Context context) {

        int nowBrightnessValue = 0;

        ContentResolver resolver = context.getContentResolver();

        try {

            nowBrightnessValue = Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return nowBrightnessValue;
    }

    // 设置亮度
    // 程序退出之后亮度失效

    public static void setCurWindowBrightness(Context context, int brightness) {

        // 如果开启自动亮度，则关闭。否则，设置了亮度值也是无效的
        if (IsAutoBrightness(context)) {
            stopAutoBrightness(context);
        }

        // context转换为Activity
        Activity activity = (Activity) context;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

        // 异常处理
        if (brightness < 1) {
            brightness = 1;
        }

        // 异常处理
        if (brightness > 255) {
            brightness = 255;
        }

        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);

        activity.getWindow().setAttributes(lp);

    }

    // 设置系统亮度
    // 程序退出之后亮度依旧有效
    public static void setSystemBrightness(Context context, int brightness) {
        // 异常处理
        if (brightness < 1) {
            brightness = 1;
        }

        // 异常处理
        if (brightness > 255) {
            brightness = 255;
        }
        saveBrightness(context, brightness);
    }

    // 停止自动亮度调节

    public static void stopAutoBrightness(Context context) {

        Settings.System.putInt(context.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    // 开启亮度自动调节

    public static void startAutoBrightness(Context context) {

        Settings.System.putInt(context.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

    }

    // 保存亮度设置状态

    public static void saveBrightness(Context context, int brightness) {

        Uri uri = Settings.System
                .getUriFor("screen_brightness");

        Settings.System.putInt(context.getContentResolver(),
                "screen_brightness", brightness);

        context.getContentResolver().notifyChange(uri, null);
    }

    /**
     * 设置当前屏幕亮度
     * @param activity
     * @param brightness
     */
    public static void setLocalScreenBright(Activity activity,int brightness){
        //设置屏幕亮度
        if(activity==null || activity.isFinishing() || brightness < 0){
            return;
        }
        Window localWindow = activity.getWindow();
        if(localWindow == null){
            return;
        }
        if (brightness < 1) {
            brightness = 1;
        } else if (brightness > 255) {
            brightness = 255;
        }
        WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
        float f = brightness / 255.0F;
        localLayoutParams.screenBrightness = f;
        try {
            localWindow.setAttributes(localLayoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
